package com.example.dmgame.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.dmgame.R;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class NewsFragment extends Fragment implements OnClickListener{
	
	private HorizontalScrollView scrollView;
	ViewPager scvPager;
	private LinearLayout btnGroup;
	Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10;
	List<Fragment> list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.news_fragment, container, false);
		button1 = (Button)view.findViewById(R.id.button_1);
		button2 = (Button)view.findViewById(R.id.button_2);
		button3 = (Button)view.findViewById(R.id.button_3);
		button4 = (Button)view.findViewById(R.id.button_4);
		button5 = (Button)view.findViewById(R.id.button_5);
		button6 = (Button)view.findViewById(R.id.button_6);
		button7 = (Button)view.findViewById(R.id.button_7);
		button8 = (Button)view.findViewById(R.id.button_8);
		button9 = (Button)view.findViewById(R.id.button_9);
		button10 = (Button)view.findViewById(R.id.button_10);
		
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);
		button5.setOnClickListener(this);
		button6.setOnClickListener(this);
		button7.setOnClickListener(this);
		button8.setOnClickListener(this);
		button9.setOnClickListener(this);
		button10.setOnClickListener(this);
		
		list = new ArrayList<Fragment>();
		
		for(int i=0;i<10;i++){
			News_listviewFragment news_listviewFragment = new News_listviewFragment();
			Bundle bundle = new Bundle();
			bundle.putInt("typeid", i);
			news_listviewFragment.setArguments(bundle);
			list.add(news_listviewFragment);
		}
		
		scrollView = (HorizontalScrollView)view.findViewById(R.id.horizontalScrollView1);
		btnGroup = (LinearLayout)view.findViewById(R.id.btnGroup);
		scvPager = (ViewPager)view.findViewById(R.id.chapter_content);
		

		scvPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
			
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return 10;
			}
			
			@Override
			public Fragment getItem(int arg0) {
				// TODO Auto-generated method stub
				return list.get(arg0);
			}
		});
		scvPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				scrollIndex(arg0);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});

		return view;
	}
	
	private void scrollIndex(int pos){
		View Btn = btnGroup.getChildAt(pos);
		int left = Btn.getLeft();
		scrollView.smoothScrollTo(left, 0);
		Log.i("NewsFragment", "---pos-->>"+pos);
		scvPager.setCurrentItem(pos);
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button_1:
			scvPager.setCurrentItem(0);
			break;
		case R.id.button_2:
			scvPager.setCurrentItem(1);
			break;
		case R.id.button_3:
			scvPager.setCurrentItem(2);
			break;
		case R.id.button_4:
			scvPager.setCurrentItem(3);
			break;
		case R.id.button_5:
			scvPager.setCurrentItem(4);
			break;
		case R.id.button_6:
			scvPager.setCurrentItem(5);
			break;
		case R.id.button_7:
			scvPager.setCurrentItem(6);
			break;
		case R.id.button_8:
			scvPager.setCurrentItem(7);
			break;
		case R.id.button_9:
			scvPager.setCurrentItem(8);
			break;
		case R.id.button_10:
			scvPager.setCurrentItem(9);
			break;
		default:
			break;
		}
	}

}
